package de.dhbw.karlsruhe.picsim.model.timer;

import de.dhbw.karlsruhe.picsim.model.cpu.FileMutable;
import de.dhbw.karlsruhe.picsim.model.memory.RegisterValue;
import de.dhbw.karlsruhe.picsim.model.memory.impl.RegisterValueImpl;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Created by IntelliJ IDEA.
 * User: goesen
 * Date: Apr 22, 2010
 * Time: 10:08:07 AM
 */

/**
 * Schreibt bei increments ins timerRegister. Das Setzen der Flags erfolgt über die Listener
 */
@SuppressWarnings({"ClassNamePrefixedWithPackageName"})
final class Timer0 extends TimerSupport {
    private final Log log = LogFactory.getLog(Timer0.class);
    private final FileMutable timerRegister;

    Timer0(final FileMutable timerRegister) {
        this.timerRegister = timerRegister;
    }


    @Override
    public void increment() {
        final int i = timerRegister.getData().asInteger();
        if (i == 255) {
            log.info("timer overflow");
            timerRegister.setValue(RegisterValueImpl.createUnsafe(0));
            fireTimeout();
        } else {
            final RegisterValue value = RegisterValueImpl.createSafe(i + 1);
            assert value != null;
            timerRegister.setValue(value);
        }
    }


}
